

import java.util.LinkedList;

public class ChessModel {
	public static final int WIDTH = 19;

	public static final int BLACK = 1;

	public static final int WHITE = -1;

	public static final int SPACE = 0;

	private int[][] data = new int[WIDTH][WIDTH];

	private LinkedList<ChessPoint> history = new LinkedList<ChessPoint>();

	public int getChess(int row, int col) {
		return data[row][col];
	}

	public void backward(){
		ChessPoint point = history.removeLast();
		data[point.row][point.col] = SPACE;
	}
	public boolean putChess(int row, int col, int color) {
		if (data[row][col] == SPACE) {
			data[row][col] = color;
			ChessPoint point = new ChessPoint(row, col, color);
			history.addLast(point);
			return true;
		}
		return false;
	}

	public int whoWin() {
		ChessPoint point = history.getLast();
		int count = 1;
		// left
		for (int i = 1; i < 5; i++) {
			if (point.col - i >= 0) {
				if (data[point.row][point.col - i] == point.color) {
					count++;
				} else {
					break;
				}
			}
		}
		// right
		for (int i = 1; i < 5; i++) {
			if (point.col + i < WIDTH) {
				if (data[point.row][point.col + i] == point.color) {
					count++;
				}else{
					break;
				}
			}
		}
		if (count >= 5) {
			return point.color;
		}
		count = 1;
		// up
		// down
		if (count >= 5) {
			return point.color;
		}
		count = 1;
		// leftup
		// rightdown
		if (count >= 5) {
			return point.color;
		}
		count=1;
		// leftdown
		// rightup

		if (count >= 5) {
			return point.color;
		}
		return SPACE;
	}
}
